let app = getApp()
import { login, updateErrorInfoState, getErrorInfo} from '../../api/getData.js'
Page({
  data: {
    username: '',
    handleState: '-1',
    eventList: [],
    page: 1,
    total: 0,
    inputValue: '',
    disTextStyle: false,
    px2rpx: 2,
    scrollTop: 0,
    isShow: false,
    index: 0,
    multiIndex: [0, 0], //选中的设备序号
    totalPoiArr: [], //获取后台数组里包含treeid和treename
    multiArray: [], //整理后，picker读取绑定数据的整体数组
    fristArr: [], //picker（一级选项）
    secondArr: [], //picker（二级选项)
    textState: 0
  },
  onLoad() {
    this.getSystem();
    let poiTotalList = app.globalData.result[0].children;
    this.setData({
      username: wx.getStorageSync('username')
    })
    console.log('app.globalData', app.globalData)
    poiTotalList.unshift({
      "treeName": "所有",
      "children": [{
        "treeName": "所有",
        "treeId": "0"
      }]
    })
    let fristArr = poiTotalList.map(item => {
      return item.treeName;
    });
    if (this.data.username !== 'decro') {
      let totalPoiArr = poiTotalList.map(item => {
        return item.children;
      });
      let secondArr = [];
      totalPoiArr.forEach(item => {
        secondArr.push(item.map(i => {
          return i.treeName;
        }));
      })

      this.setData({
        multiArray: [fristArr, secondArr[0]],//弹出时，默认二级是所有
        secondArr: secondArr,
        fristArr: fristArr,
        totalPoiArr: totalPoiArr
      })
    }else{
      this.setData({
        fristArr: fristArr,
      })
    }
   
  },

  onShow() {
    this.getErrorList(this.data.handleState,'0', 1);
  },

  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
      scrollTop: 0
    });
    let treeID = app.globalData.result[0]['children'][1]['treeID'];
    this.getErrorList(this.data.handleState, treeID, 1);
  },

  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value
    });
    let { handleState, totalPoiArr } = this.data;
    if (!e.detail.value[0] && !e.detail.value[1]) {
      this.setData({
        eventList: [],
        textState: 0,
        scrollTop: 0
      })
      this.getErrorList(handleState, 0, 1);
    }else{
      let treeID = totalPoiArr[e.detail.value[0]][e.detail.value[1]]['treeID'];
      this.setData({
        eventList: [],
        textState: 1,
        scrollTop: 0
      })
      this.getErrorList(handleState, treeID, 1);
    } 
  },
  bindMultiPickerColumnChange: function (e) {
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        data.multiArray[1] = this.data.secondArr[data.multiIndex[0]] 
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
    }
    this.setData(data);
  },

  getSystem: function () {
    var self = this;
    wx.getSystemInfo({
      success: function (res) {
        self.setData({
          px2rpx: 750 / res.windowWidth
        })
      },
    });
  },

  selectTitle(e) {
    let handleState = e.target.dataset.state;
    if (this.data.handleState === handleState) return;
    this.setData({
      handleState: handleState,
      page: 1,
      eventList:[],
      total: 0,
      scrollTop: 0
    });
    this.getErrorList(this.data.handleState,'0',1);
  },

  checkboxChange(e) {
    let message = this.data.inputValue ? this.data.inputValue : e.currentTarget.dataset.inputvalue;
    if (e.currentTarget.dataset.handlestate === 0){
      if (!this.data.inputValue && !e.currentTarget.dataset.inputvalue) {
        wx.showToast({
          title: '请输入备注',
          icon: 'none'
        })
        return false;
      }
      this.updateErrorInfo(1, e.currentTarget.dataset.eventid, message);
      this.data.eventList[e.currentTarget.dataset.id].disTextStyle = true;
      this.data.eventList[e.currentTarget.dataset.id].handleState = 1;
      this.setData({
        eventList: this.data.eventList
      })
    } 
    if (e.currentTarget.dataset.handlestate === 1) {
      this.updateErrorInfo(0, e.currentTarget.dataset.eventid, message);
      this.data.eventList[e.currentTarget.dataset.id].disTextStyle = false;
      this.data.eventList[e.currentTarget.dataset.id].handleState = 0;
      this.setData({
        eventList: this.data.eventList
      })
    }
  },
   
  // 备注
  bindKeyInput(e) {
    this.setData({
      inputValue: e.detail.value
    })
    this.data.eventList[e.currentTarget.dataset.id].handleMessage = this.data.inputValue;
    this.setData({
      eventList: this.data.eventList
    })
  },


  // 页面上拉加载更多
  loadMoreList() {
    let eventLength = this.data.eventList.length;
    if (eventLength) {
      if (eventLength >= this.data.total) {
        wx.showToast({
          title: '已经到底了',
          icon: 'none'
        })
        return
      }
      this.setData({
        page: ++this.data.page
      })
      this.getErrorList(this.data.handleState, '0',this.data.page);
    }
  },

  getErrorList(handleState,treeID,page) {
    wx.showLoading({
      title: '加载中',
    })
    getErrorInfo(handleState,treeID,page)
      .then(res => {
        wx.hideLoading();
        if (res.resCode === "SUCCESS") {
          let newEventList = res.result.eventList;
          newEventList.forEach((item,index) => {  
            if (item.handleState === 1) { 
              item.disTextStyle = true; 
              item.id = index 
              };
            if (item.handleState === 0) { 
              item.disTextStyle = false
              item.id = index 
              };
          })
          this.setData({
            eventList: this.data.eventList.concat(...newEventList),
            total: res.result.counts,
          });
        }  
      })                                                                                                           },
  updateErrorInfo(handleState, eventID, message) {
    wx.showLoading({
      title: '加载中',
    })
    updateErrorInfoState(handleState, eventID, message)
      .then(res => {
        wx.hideLoading();
        if (res.resCode === "SUCCESS") {
          wx.showToast({
            title: '状态已改变',
            icon: 'none'
          })
        }
      })
  }
})